#!/bin/bash

set -e

cat<<EOF

#######################################
#  INSTALL PREREQUISITES
#######################################

EOF

download () {
	if [ ! -f $1 ]; then
		wget -c -O $1 $2
	fi
}

# add mono repo
rpm --import repo-mono.key

yum -y install yum-utils


REV=$(cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//);
REV_PARTS=(${REV//\./ });
REV=${REV_PARTS[0]};
hyperfastcgi_version="0.4-4"

if ! [[ "$REV" =~ ^[0-9]+$ ]]; then
	REV=7;
fi

#su -c "curl https://download.mono-project.com/repo/centos$REV-stable.repo | tee /etc/yum.repos.d/mono-centos$REV-stable.repo"
cp mono-centos$REV-stable.repo /etc/yum.repos.d/mono-centos$REV-stable.repo

# add mysql repo
download mysql57-community-release-el7-9.noarch.rpm https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
yum localinstall -y mysql57-community-release-el7-9.noarch.rpm || true

# add epel repo
download epel-release-latest-$REV.noarch.rpm https://dl.fedoraproject.org/pub/epel/epel-release-latest-$REV.noarch.rpm
rpm -ivh epel-release-latest-$REV.noarch.rpm || true
download remi-release-$REV.rpm https://rpms.remirepo.net/enterprise/remi-release-$REV.rpm
rpm -ivh remi-release-$REV.rpm || true
# add nginx repo
cat > /etc/yum.repos.d/nginx.repo <<END
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$REV/\$basearch/
gpgcheck=1
gpgkey=https://nginx.org/keys/nginx_signing.key
enabled=1
END

if [ "$REV" = "8" ]; then

hyperfastcgi_version="0.4-7";

rpm --import cs.key || true

cat > /etc/yum.repos.d/mono-extra.repo <<END
[mono-extra]
name=mono-extra repo
baseurl=https://d2nlctn12v279m.cloudfront.net/repo/mono/centos/main/noarch/
gpgcheck=1
enabled=1
END

cat > /etc/yum.repos.d/rabbitmq-server.repo <<END
[rabbitmq-server]
name=rabbitmq-server
baseurl=https://packagecloud.io/rabbitmq/rabbitmq-server/el/7/\$basearch
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey
sslverify=0
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
END

dnf remove -y @mysql
dnf module -y reset mysql && dnf module -y disable mysql
fi

# add nodejs repo
bash nodejs_10.x.sh || true

yum -y install epel-release \
			python36 \
			python-pip \
			expect \
			nano \
			htop \
			supervisor \
			postgresql \
			postgresql-server \
			rabbitmq-server \
			redis --enablerepo=remi \
			mysql-community-server \
			mysql-community-client \
			mono-webserver-hyperfastcgi-$hyperfastcgi_version \
			mono-complete-6.8.0.123-0.xamarin.2.epel$REV \
			java-1.8.0-openjdk-headless

if [ "$REV" = "7" ]; then
	yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional
	yum -y install certbot-nginx
fi

if [ "$REV" = "8" ]; then
    download certbot-auto https://dl.eff.org/certbot-auto
    cp certbot-auto /usr/bin/certbot
    chown root /usr/bin/certbot
    chmod 0755 /usr/bin/certbot
	certbot --install-only --non-interactive
fi

download rpmfusion-free-release-$REV.noarch.rpm https://download1.rpmfusion.org/free/el/rpmfusion-free-release-$REV.noarch.rpm
yum localinstall -y --nogpgcheck rpmfusion-free-release-$REV.noarch.rpm

download SDL2-2.0.10-1.el7.x86_64.rpm http://rpmfind.net/linux/epel/7/x86_64/Packages/s/SDL2-2.0.10-1.el7.x86_64.rpm
yum localinstall -y --nogpgcheck SDL2-2.0.10-1.el7.x86_64.rpm

if rpm -q ffmpeg2; then
	yum -y remove ffmpeg2
fi

yum -y install ffmpeg ffmpeg-devel

# python3 get-pip.py || true
# download pip-20.1.1-py2.py3-none-any.whl https://files.pythonhosted.org/packages/43/84/23ed6a1796480a6f1a2d38f2802901d078266bda38388954d01d3f2e821d/pip-20.1.1-py2.py3-none-any.whl
# download wheel-0.34.2-py2.py3-none-any.whl https://files.pythonhosted.org/packages/8c/23/848298cccf8e40f5bbb59009b32848a4c38f4e7f3364297ab3c3e2e2cd14/wheel-0.34.2-py2.py3-none-any.whl

postgresql-setup initdb || true

if ! rpm -q elasticsearch; then
	download elasticsearch-6.5.0.rpm http://upload.liuzy88.com/elasticsearch-6.5.0.rpm
	rpm -Uvh elasticsearch-6.5.0.rpm
fi

semanage permissive -a httpd_t

package_services="rabbitmq-server postgresql redis mysqld supervisord elasticsearch"